<?php
namespace common\models;

use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;

/**
 * 订单模型
 * @author WJ
 */
class AROrder extends ActiveRecord
{
    const TYPE_IN = 1;
    const TYPE_OUT = 2;

    const STATUS_CANCEL = -1; //交易取消
    const STATUS_COMMUNICATION = 2; //交流中
    const STATUS_START = 4; //三方确认，正式交易中
    const STATUS_BUYER_TRANSFER = 5; //买家已确认转款
    const STATUS_BROKER_RECEIVE = 6; //经纪人已确认收款
    // const STATUS_BUYER_RECEIVE = 7; //买家确认收币
    const STATUS_BROKER_TRANSFER = 8; //经纪人已确认转款
    const STATUS_SUCCESS = 9; //卖家已确认收款，交易完成

    const IDENTITY_BUY = 1; //买方
    const IDENTITY_SELL = 2; //卖方
    const IDENTITY_BROKER = 3; //经纪人

    const USER_STATUS_CANCEL = -2;//撤销
    const USER_STATUS_REFUSE = -1; //拒绝
    const USER_STATUS_UNCOMFIRM = 1; //未确认
    const USER_STATUS_CONFIRM = 99; //同意

    const ACTION_TYPE_LAUNCH = 1; //发起方
    const ACTION_TYPE_ACCEPT = 2; //接收方
    const ACTION_TYPE_BROKE = 3; //经纪业务方

    const SECURITY_SAFE = 1;
    const SECURITY_UNSAFE = 0;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%order}}';
    }

    public function behaviors()
    {
        return [
            TimestampBehavior::className(),
        ];
    }

    public function rules()
    {
        return [
            [['u_status', 'opp_status', 'broker_status'], 'default', 'value' => self::USER_STATUS_UNCOMFIRM],
        ];
    }

    /**
     * 获取交易发布用户信息
     */
    public function getUser()
    {
        return $this->hasOne(ARUser::className(), ['id' => 'uid']);
    }

    /**
     * 获取对方用户信息
     */
    public function getOppUser()
    {
        return $this->hasOne(ARUser::className(), ['id' => 'opp_uid']);
    }

    /**
     * 获取虚拟币信息
     */
    public function getCoinType()
    {
        return $this->hasOne(ARCoinType::className(), ['id' => 'coin']);
    }

    /**
     * 获取虚拟币信息
     */
    public function getCurrencyType()
    {
        return $this->hasOne(ARCurrencyType::className(), ['id' => 'currency']);
    }

    /**
     * 获取手续费信息
     */
    public function getServiceCharge()
    {
        return $this->hasOne(ARServiceCharge::className(), ['order_id' => 'id', 'uid' => 'broker']);
    }
}
